# 接受一个整数数字 n
# 返回一个字符串，这个字符串包含的是用空格分隔的每一个数字
# read_digits(1234) -> "1 2 3 4"
# read_digits(101) -> "1 0 1"
# read_digits(12) -> "1 2"
# 1 <= n <= 9999 
# 1994
# result = ""
# 1994 // 1000 = 1 -> 得到千位数字了 -> 添加到 result + " "
# 1994 % 1000 = 994
# 994 // 100 = 9   -> 得到百位数字了 -> 添加到 result + " "
# 994 % 100 = 94
# 94 // 10 = 9      -> 得到十位数字了 -> 添加到 result + " "
# 94 % 10 = 4
# 4 // 1 = 4        -> 得到各位数字了 -> 添加到 result
def read_digits(n):
  result = ""
  # 处理千位
  quotient = n // 1000
  need_to_read_zero = False

  if quotient != 0:
    result += f"{quotient} "
    need_to_read_zero = True

  n = n % 1000

  # 处理百位
  quotient = n // 100

  if quotient != 0:
    result += f"{quotient} "
    need_to_read_zero = True
  elif need_to_read_zero:
    result += "0 "

  n = n % 100

  # 处理十位
  quotient = n // 10

  if quotient != 0:
    result += f"{quotient} "
    need_to_read_zero = True
  elif need_to_read_zero:
    result += "0 "

  n = n % 10

  # 处理个位
  quotient = n
  result += f"{quotient} "

  return result
  # TODO:
  # 1. 处理十位和各位的数字
  # 2. 测试 1234 234 34 4 这 4 个数字的打印结果
  # 3. 测试 1021 1201 1200 这 3 个数字的打印结果，如果结果不正确，尝试修改这段代码
  




chinese = {
  1: "yi", 2: "er", 3: "san", 4: "si", 5: "wu",
  6: "liu", 7: "qi", 8: "ba", 9: "jiu", 0: "ling"
}

def read_num(n):
  result = ""
  is_read_zero = False
  need_to_read_zero = False
  
	# 处理千位
	# 计算千位数字
  r = n // 1000
	# 计算剩余数字
  n %= 1000
  if r != 0:
    # We never read the leading 0
    result += f"{chinese[r]} qian "
    if n != 0:
			# 剩余的数字不等于 0，说明接下来我们有可能需要读 0。
      need_to_read_zero = (n != 0)
  
	# 处理百位
  r = n // 100
  n %= 100
  if r != 0:
    result += f"{chinese[r]} bai "
    need_to_read_zero = (n != 0)
  elif (not is_read_zero) and need_to_read_zero:
    result += "ling "
    is_read_zero = True
  
	# 处理十位
  r = n // 10
  n %= 10
  if r != 0:
    result += f"{chinese[r]} shi "
  elif not is_read_zero and need_to_read_zero:
    result += "ling "
  
	# 处理个位
  # We never read this trailing zero
  if n != 0:
    result += f"{chinese[n]} "
    
  return result

#
def solution(n):
  if n == 0:
    return chinese[0]
  
  result = ""
  need_to_read_zero = False
  
  # 计算有多少亿
  quotient = n // 100000000
  if quotient != 0:
    # 如果以亿为单位计算的结果不为 0，说明后面我们有可能需要读 0。
    result += f"{read_num(quotient)}yi "
    need_to_read_zero = True
  
  # 去掉亿以上的部分
  n = n % 100000000
  # 算剩余的部分有多少万
  quotient = n // 10000

  # case 1 正常读出结果
  if quotient >= 1000:
    result += f"{read_num(quotient)}wan "
    need_to_read_zero = True
  # case 2 如果不够千万，则需要在百万前面读 0
  elif quotient != 0 and need_to_read_zero:
    result += f"ling {read_num(quotient)}wan "
    need_to_read_zero = True
  # case 3 否则，表示这是一个小于 1 亿的数字，我们不要读前导 0
  elif quotient != 0:
    result += f"{read_num(quotient)}wan "
    need_to_read_zero = True
  
  # 去掉万以上的部分
  n = n % 10000
  # 用处理万以上部分的 case 1 2 3 处理
  if n >= 1000:
    result += f"{read_num(n)} "
  elif n != 0 and need_to_read_zero:
    result += f"ling {read_num(n)} "
  elif n != 0:
    result += f"{read_num(n)} "
    
  return result

while True:
  n = input("Input a number to read: ")
  n = int(n)
  print(n)
  print(read_digits(n))
